package com.chenglai.goods.controller;

import com.chenglai.commonutils.common.Result;
import com.chenglai.commonutils.common.StatusCode;
import com.chenglai.goods.pojo.GoodsCart;
import com.chenglai.goods.service.GoodsCartService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 类描述：购物车
 *
 * @Author rxh
 * @Date 2023/11/17 14:35
 */
@RestController("/goodsCart/")
public class GoodsCartController {

    @Resource
    private GoodsCartService goodsCartService;

    /**
     * 获取购物车信息
     * @param userId 用户ID
     * @return
     */
    @PostMapping("getGoodsCartList")
    @ApiOperation(value = "获取购物车信息")
    public Result getGoodsCartList(@ApiParam(name = "userId", value = "用户ID")Integer userId){
        try {
            List<GoodsCart> list = goodsCartService.getGoodsCartList(userId);
            return new Result(true, StatusCode.OK, "操作成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }

    /**
     * 添加商品至购物车
     * @param goodsCart 商品信息
     * @return
     */
    @PostMapping("getGoodsCartList")
    @ApiOperation(value = "添加商品至购物车")
    public Result addGoodsToGoodsCart(GoodsCart goodsCart) {
        try {
            Map<String,Object> data = goodsCartService.addGoodsToGoodsCart(goodsCart);
            return new Result(true, StatusCode.OK, "操作成功",data);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }

    /**
     * 删除购物车的商品
     *
     * @param ids 购物车id
     */
    @PostMapping("delGoodsSpec")
    @ApiOperation(value = "删除购物车的商品")
    public Result delArticleCategory(@ApiParam(name = "ids", value = "购物车id") String ids) {
        try {
            goodsCartService.delGoodsCart(ids);
            return new Result(true, StatusCode.OK, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }

    /**
     * 购物车商品详情
     *
     * @param cartId 购物车id
     */
    @PostMapping("detail")
    @ApiOperation(value = "购物车商品详情")
    public Result detail(@ApiParam(name = "specId", value = "商品规格ID") Integer cartId) {
        try {
            GoodsCart detail = goodsCartService.detail(cartId);
            return new Result(true, StatusCode.OK, "查询成功", detail);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }

}
